#include <sstream>
#include <fstream>
#include <iostream>
#include <stdexcept>
using namespace std;

namespace noria { namespace io {

	class FileImpl final
	{
	public:
		void OpenRead(const char* file, const char* encoding/* = "utf-8"*/)
		{
			m_fs.open(file, ios_base::in);
			if (m_fs.fail())
			{
				stringstream ss;
				ss << "failed to open file(r) " << file << endl;
				throw runtime_error(ss.str());
			}
			cout << "File::OpenRead -> " << file << endl;
		}
		void OpenWrite(const char* file, const char* encoding/* = "utf-8"*/, bool append/* = false*/)
		{
			auto mode = ios_base::out | (append ? ios_base::app : ios_base::trunc);
			m_fs.open(file, mode);
			if (m_fs.fail())
			{
				stringstream ss;
				ss << "failed to open file(w) " << file << endl;
				throw runtime_error(ss.str());
			}
			cout << "File::OpenWrite -> " << file << endl;
		}
		void Close()
		{
			m_fs.close();
			cout << "File::Close" << endl;
		}
		FileImpl* operator<<(const char* val)
		{
			m_fs << val;
			return this;
		}
		FileImpl* operator<<(const wchar_t* val)
		{
			m_fs << val;
			return this;
		}
		FileImpl* operator<<(int val)
		{
			m_fs << val;
			return this;
		}
	private:
		fstream m_fs;
	};
}}